package com.youlai.boot.modules.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.modules.game.mapper.GameUserRecordMapper;
import com.youlai.boot.modules.game.model.entity.GameUserRecord;
import com.youlai.boot.modules.game.model.form.GameUserRecordForm;
import com.youlai.boot.modules.game.model.query.GameUserRecordQuery;
import com.youlai.boot.modules.game.model.vo.GameUserRecordVO;
import com.youlai.boot.modules.game.service.GameUserRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 游戏用户记录服务实现类
 *
 * @author 有来技术团队
 */
@Service
@RequiredArgsConstructor
public class GameUserRecordServiceImpl extends ServiceImpl<GameUserRecordMapper, GameUserRecord> implements GameUserRecordService {

    @Override
    public Page<GameUserRecordVO> getUserGameRecords(GameUserRecordQuery query) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (query.getUserId() == null) {
            query.setUserId(SecurityUtils.getUserId());
        }
        
        // 创建分页对象
        Page<GameUserRecord> page = new Page<>(query.getPageNum(), query.getPageSize());
        
        // 调用Mapper进行分页查询
        return baseMapper.getUserGameRecords(page, query);
    }

    @Override
    public GameUserRecordVO getUserGameRecord(Long id) {
        // 根据ID查询游戏记录
        GameUserRecord record = this.getById(id);
        if (record == null) {
            return null;
        }
        
        // 查询记录的详细信息并返回
        Page<GameUserRecord> page = new Page<>(1, 1);
        GameUserRecordQuery query = new GameUserRecordQuery();
        query.setUserId(record.getUserId());
        query.setRecordId(id);
        
        Page<GameUserRecordVO> resultPage = baseMapper.getUserGameRecords(page, query);
        if (resultPage.getRecords().isEmpty()) {
            // 如果没有找到对应记录，则返回基本数据
            GameUserRecordVO vo = new GameUserRecordVO();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }
        
        return resultPage.getRecords().get(0);
    }

    @Override
    public boolean updateUserGameRecord(Long id, GameUserRecordForm form) {
        // 查询原记录
        GameUserRecord record = this.getById(id);
        if (record == null) {
            return false;
        }
        
        // 更新记录内容
        BeanUtils.copyProperties(form, record);
        record.setUpdateTime(LocalDateTime.now());
        
        // 保存更新
        return this.updateById(record);
    }

    @Override
    public Long saveUserGameRecord(GameUserRecordForm form) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (form.getUserId() == null) {
            form.setUserId(SecurityUtils.getUserId());
        }
        
        // 查询是否已存在该用户的游戏记录
        LambdaQueryWrapper<GameUserRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GameUserRecord::getUserId, form.getUserId())
               .eq(GameUserRecord::getGameId, form.getGameId());
        
        GameUserRecord record = this.getOne(wrapper);
        boolean isNew = false;
        
        if (record == null) {
            // 创建新记录
            record = new GameUserRecord();
            record.setCreateTime(LocalDateTime.now());
            record.setStartDate(LocalDateTime.now());
            isNew = true;
        }
        
        // 更新记录内容
        BeanUtils.copyProperties(form, record);
        record.setUpdateTime(LocalDateTime.now());
        record.setLastPlayed(LocalDateTime.now());
        
        // 保存记录
        if (isNew) {
            this.save(record);
        } else {
            this.updateById(record);
        }
        
        return record.getId();
    }

    @Override
    public List<Map<String, Object>> getGameTypeDistribution(Long userId) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 查询用户游戏类型分布
        return baseMapper.getGameTypeDistribution(userId);
    }

    @Override
    public List<Map<String, Object>> getGameProgressList(Long userId, Integer limit) {
        // 如果未提供用户ID，则使用当前登录用户ID
        if (userId == null) {
            userId = SecurityUtils.getUserId();
        }
        
        // 设置默认限制数量
        if (limit == null || limit <= 0) {
            limit = 10;
        }
        
        // 查询用户游戏进度列表
        return baseMapper.getGameProgressList(userId, limit);
    }

    @Override
    public boolean incrementPlayTime(Long id, Integer playTimeIncrement) {
        // 参数校验
        if (id == null || playTimeIncrement == null || playTimeIncrement <= 0) {
            return false;
        }
        
        // 更新游戏时长
        int rows = baseMapper.incrementPlayTime(id, playTimeIncrement);
        
        return rows > 0;
    }
} 